﻿using BaseDll;
using MotionIoLib;
using Newtonsoft.Json;
using OtherDevice;
using System;
using System.ComponentModel;
using System.Threading;

namespace ModuleUnit
{
    public class Config_TrayIn
    {
        [Category("工站参数"), DisplayName("1.进料请求 "), Description("进料请求 ")]
        public string str_进料请求 { set; get; } = "";
        [Category("工站参数"), DisplayName("2.进料完成"), Description("进料完成")]
        public string str_进料完成 { set; get; } = "";
        [Category("工站参数"), DisplayName("3.进料许可"), Description("进料许可")]
        public string str_进料许可 { set; get; } = "";
        [Category("工站参数"), DisplayName("4.进OK料"), Description("进OK料")]
        public string str_进OK料 { set; get; } = "";
        [Category("工站参数"), DisplayName("5.进NG料"), Description("进NG料")]
        public string str_进NG料 { set; get; } = "";
        [Category("工站参数"), DisplayName("5.后段完成"), Description("后段完成")]
        public bool b_后段完成 { set; get; } = true;
        [Category("工站参数"), DisplayName("6.流水线式"), Description("流水线式")]
        public bool b_流水线式 { set; get; } = true;
        [Category("工站参数"), DisplayName("7.滤波时长"), Description("滤波时长")]
        public int n_滤波时长 { set; get; } = 20;
    }
    [Description("进料模块")]
    public class Module_TrayIn : ModuleBase
    {
        public Config_TrayIn config = null;
        public Module_TrayIn(object config, string strName) : base(strName)
        {
            if (config == null)
            {
                this.config = new Config_TrayIn();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_TrayIn>(config.ToString());

            }
            this.obj_Config = this.config;
        }
        public bool Function_等待进料开始(bool bmanual)
        {
            if (!config.b_后段完成)
            {
                config.str_进料请求.SetIO(true);
                b_InputIOState = true;
            }
                
            while (true)
            {
                if(bmanual)
                {
                    if (GlobalVariable.g_StationState != StationState.StationStateStop)
                        throw new Exception("手动停止");
                }
                else
                {
                    if (GlobalVariable.g_StationState != StationState.StationStateRun&& GlobalVariable.g_StationState != StationState.StationStatePause)
                        throw new Exception("自动停止");
                }
                if (BaseDll.GlobalVariable.g_StationState == BaseDll.StationState.StationStatePause&&!config.b_流水线式)
                    continue;
                if (IOMgr.GetInstace().Function_ReCheckIO(config.str_进料许可, true, config.n_滤波时长))
                    break;
                Thread.Sleep(5);
            }
            bool b_Result = true;
            if (config.str_进OK料 != "")
                b_Result = config.str_进OK料.GetInput();
            if (config.b_后段完成)
            {
                config.str_进料完成.SetIO(false);
                config.str_进料请求.SetIO(true);
                b_InputIOState = true;
            }
            else
            {
                config.str_进料请求.SetIO(false);
                b_InputIOState = false;
            }
            return b_Result;

        }
        public void Function_进料完成(bool bmanual)
        {
            if (config.b_后段完成)
            {
                config.str_进料请求.SetIO(false);
                config.str_进料完成.SetIO(true);
                b_InputIOState = false;
            }
            else
            {
                while (true)
                {
                    if (bmanual)
                    {
                        if (GlobalVariable.g_StationState != StationState.StationStateStop)
                            throw new Exception("手动停止");
                    }
                    else
                    {
                        if (GlobalVariable.g_StationState != StationState.StationStateRun && GlobalVariable.g_StationState != StationState.StationStatePause)
                            throw new Exception("自动停止");
                    }

                    if (IOMgr.GetInstace().Function_ReCheckIO(config.str_进料完成, true, config.n_滤波时长))
                        break;
                    Thread.Sleep(5);
                }

            }
        }

        bool b_InputIOState = false;
        public void PauseDeal()
        {
            b_InputIOState = config.str_进料请求.GetOutput();
            config.str_进料请求.SetIO(false);

        }
        public void ResumeDeal()
        {
            config.str_进料请求.SetIO(b_InputIOState);

        }
        public void StopDeal()
        {
            if (config.str_进料请求 != "")
                config.str_进料请求.SetIO(false);
            if (config.str_进料请求 != "" && config.b_后段完成)
                config.str_进料完成.SetIO(false);
        }

    }
}